using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;

namespace OgilvyOne.SqlServerDAL
{
    public class Role : IRole
    {
        // Fields
        private const string PARM_ISDEL = "@IsDel";
        private const string PARM_ROLE_CREATE_DATE = "@Role_Create_Date";
        private const string PARM_ROLE_DESC = "@Role_Desc";
        private const string PARM_ROLE_NAME = "@Role_Name";
        private const string PARM_ROLE_UID = "@Role_UID";
        private const string PARM_ROLE_UPDATE_DATE = "@Role_Update_Date";
        private const string SQL_DELETE_ROLE = "UPDATE Sys_Role SET IsDel = '1' WHERE Role_UID = @Role_UID";
        private const string SQL_INSERT_ROLE = "INSERT INTO Sys_Role VALUES(@Role_Name,@Role_Desc,getDate(),null,'0')";
        private const string SQL_SELECT_ROLE = "SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where Role_UID=@Role_UID and IsDel='0'";
        private const string SQL_SELECT_ROLE_LIST = "SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where IsDel='0'";
        private const string SQL_SELECT_ROLE_SEARCHLIST = "SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where IsDel='0' and Role_Name like '%{0}%' and Role_UID=@Role_UID";
        private const string SQL_SELECT_ROLE_SEARCHNAMELIST = "SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where IsDel='0' and Role_Name like '%{0}%'";
        private const string SQL_UPDATE_ROLE = "UPDATE Sys_Role SET Role_Name = @Role_Name, Role_Desc = @Role_Desc, Role_Update_Date = getDate() WHERE Role_UID = @Role_UID";

        // Methods
        public void Delete(int role_id)
        {
            SqlParameter[] roleDeleteParameters = GetRoleDeleteParameters();
            roleDeleteParameters[0].Value = role_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Role SET IsDel = '1' WHERE Role_UID = @Role_UID", roleDeleteParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        private static SqlParameter[] GetRoleDeleteParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Role SET IsDel = '1' WHERE Role_UID = @Role_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Role_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Role SET IsDel = '1' WHERE Role_UID = @Role_UID", cachedParameters);
            }
            return cachedParameters;
        }

        private static SqlParameter[] GetRoleInsertParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("INSERT INTO Sys_Role VALUES(@Role_Name,@Role_Desc,getDate(),null,'0')");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Role_Name", SqlDbType.VarChar, 0x40), new SqlParameter("@Role_Desc", SqlDbType.VarChar, 0x80) };
                SQLHelper.CacheParameters("INSERT INTO Sys_Role VALUES(@Role_Name,@Role_Desc,getDate(),null,'0')", cachedParameters);
            }
            return cachedParameters;
        }

        public IList getRoleList()
        {
            IList list = new ArrayList();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where IsDel='0'", null))
            {
                while (reader.Read())
                {
                    RoleInfo info = new RoleInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getRoleList(int roleid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Role_UID", SqlDbType.Int, 4, "role_uid") };
            cmdParms[0].Value = roleid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where Role_UID=@Role_UID and IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    RoleInfo info = new RoleInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getRoleList(string role_Name)
        {
            IList list = new ArrayList();
            string cmdText = string.Format("SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where IsDel='0' and Role_Name like '%{0}%'", role_Name);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    RoleInfo info = new RoleInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getRoleList(int roleid, string role_Name)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Role_UID", SqlDbType.Int, 4, "role_uid") };
            cmdParms[0].Value = roleid;
            string cmdText = string.Format("SELECT Role_UID,Role_Name,Role_Desc,Role_Create_Date,Role_Update_Date FROM Sys_Role where IsDel='0' and Role_Name like '%{0}%' and Role_UID=@Role_UID", role_Name);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    RoleInfo info = new RoleInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetRoleModifyParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Role SET Role_Name = @Role_Name, Role_Desc = @Role_Desc, Role_Update_Date = getDate() WHERE Role_UID = @Role_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Role_UID", SqlDbType.Int, 4), new SqlParameter("@Role_Name", SqlDbType.VarChar, 0x40), new SqlParameter("@Role_Desc", SqlDbType.VarChar, 0x80) };
                SQLHelper.CacheParameters("UPDATE Sys_Role SET Role_Name = @Role_Name, Role_Desc = @Role_Desc, Role_Update_Date = getDate() WHERE Role_UID = @Role_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public void Insert(RoleInfo acc)
        {
            SqlParameter[] roleInsertParameters = GetRoleInsertParameters();
            this.SetRoleInsertParameters(roleInsertParameters, acc);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "INSERT INTO Sys_Role VALUES(@Role_Name,@Role_Desc,getDate(),null,'0')", roleInsertParameters);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        private void SetRoleInsertParameters(SqlParameter[] parms, RoleInfo acc)
        {
            parms[0].Value = acc.Role_Name;
            parms[1].Value = acc.Role_Desc;
        }

        private void SetRoleModifyParameters(SqlParameter[] parms, RoleInfo acc)
        {
            parms[0].Value = acc.Role_UID;
            parms[1].Value = acc.Role_Name;
            parms[2].Value = acc.Role_Desc;
        }

        public void Update(RoleInfo myItem)
        {
            SqlParameter[] roleModifyParameters = GetRoleModifyParameters();
            this.SetRoleModifyParameters(roleModifyParameters, myItem);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Role SET Role_Name = @Role_Name, Role_Desc = @Role_Desc, Role_Update_Date = getDate() WHERE Role_UID = @Role_UID", roleModifyParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
